package top.wyloop.intoeins.module.iam.initializer;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Component;
import top.wyloop.intoeins.module.iam.model.consts.InitRoleConst;
import top.wyloop.intoeins.module.iam.repository.RoleRepository;

import java.util.Date;

/**
 * @author Wyatt6
 * @date 2025/2/1
 */
@Slf4j
@Component
@Order(2)
public class RoleInitializer implements CommandLineRunner {
    @Autowired
    private RoleRepository repository;

    @Override
    public void run(String... args) throws Exception {
        log.info("[[加载系统角色（超级管理员、管理员）数据]]");
        try {
            repository.insert(
                    InitRoleConst.SUPER_ADMIN_ROLE_ID,
                    InitRoleConst.SUPER_ADMIN_ROLE,
                    InitRoleConst.SUPER_ADMIN_ROLE_NAME, 1,
                    InitRoleConst.SUPER_ADMIN_ROLE_ID, new Date(), null);
            log.info("已加载: {} {}", InitRoleConst.SUPER_ADMIN_ROLE, InitRoleConst.SUPER_ADMIN_ROLE_NAME);
        } catch (DataIntegrityViolationException e) {
            log.info("已存在: {} {}", InitRoleConst.SUPER_ADMIN_ROLE, InitRoleConst.SUPER_ADMIN_ROLE_NAME);
        }
        try {
            repository.insert(
                    InitRoleConst.ADMIN_ROLE_ID,
                    InitRoleConst.ADMIN_ROLE,
                    InitRoleConst.ADMIN_ROLE_NAME, 1,
                    InitRoleConst.ADMIN_ROLE_ID, new Date(), null);
            log.info("已加载: {} {}", InitRoleConst.ADMIN_ROLE, InitRoleConst.ADMIN_ROLE_NAME);
        } catch (DataIntegrityViolationException e) {
            log.info("已存在: {} {}", InitRoleConst.ADMIN_ROLE, InitRoleConst.ADMIN_ROLE_NAME);
        }
    }
}
